package com.ysg.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.http.data.entity.mine.SignResult;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.adapter.SignAdapter;

/**
 * 签到
 */
public class SignDialog extends BaseDialog {
    public interface OnSignListener{
        void onSwitchClick();
        void onSignClick();
    }
    private OnSignListener listener;
    public void setOnSignListener(OnSignListener listener){
        this.listener = listener;
    }

    private SignResult data;

    private RecyclerView recyclerView;
    private SignAdapter adapter;
    private TextView tvSignDay, btnSign;
    private ImageView ivClose, ivSwitch;
    private View viewSwitch;

    public SignDialog(Context context, SignResult data) {
        super(context);
        this.data = data;
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_sign;
    }

    public void initView() {
        tvSignDay = findViewById(R.id.tvSignDay);
        btnSign = findViewById(R.id.btnSign);
        ivSwitch = findViewById(R.id.ivSwitch);
        viewSwitch = findViewById(R.id.viewSwitch);
        ivClose = findViewById(R.id.ivClose);

        recyclerView = findViewById(R.id.recyclerView);
        adapter = new SignAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 6) {
                    return 2;
                }
                return 1;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        String days = String.format(getContext().getResources().getString(R.string.dialog_sign_day), data.getOnDay());
        tvSignDay.setText(days);
        if (YStringUtil.eq("1", data.getAlert())) {
            ivSwitch.setImageResource(R.mipmap.mine_switch_open);
        } else {
            ivSwitch.setImageResource(R.mipmap.mine_switch_close);
        }
        if (YStringUtil.eq("1", data.getTodaySigned())) {
            btnSign.setText(getContext().getResources().getString(R.string.dialog_sign_select));
            btnSign.setEnabled(false);
            btnSign.setBackgroundResource(R.drawable.shape_gray_radius);
        } else {
            btnSign.setText(getContext().getResources().getString(R.string.dialog_sign_unselect));
        }

        adapter.setData(data.getTodaySigned()+"", data.getOnDay()+"");
        adapter.setList(data.getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initListener() {
        ivClose.setOnClickListener((v -> {
            dismiss();
        }));
        viewSwitch.setOnClickListener((v -> {
            if (listener != null){
                listener.onSwitchClick();
                if (YStringUtil.eq("1", data.getAlert())) {
                    data.setAlert(0);
                    ivSwitch.setImageResource(R.mipmap.mine_switch_close);
                } else {
                    data.setAlert(1);
                    ivSwitch.setImageResource(R.mipmap.mine_switch_open);
                }
            }
        }));
        btnSign.setOnClickListener((v -> {
            dismiss();
            if (listener != null) listener.onSignClick();
        }));





    }


}
